import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;


import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

//Saves the Inventory to .html file
public class ChrisPerez {

	String bullpenDir;

	JFileChooser cp = new JFileChooser();

	public ChrisPerez(ArrayList<InventoryItem> inventoryList, String loc, String per){
		//File save = new File(bullpenDir);
		JFrame chooserFrame = new JFrame();
		JFileChooser chooser = new JFileChooser();

		chooser.setApproveButtonText("Save");
		int returnVal =  chooser.showOpenDialog(chooserFrame);

		if(returnVal == JFileChooser.APPROVE_OPTION){
		
			bullpenDir = chooser.getSelectedFile().toString();

//			System.out.println("This is the directory: " + bullpenDir);

			BufferedWriter html = null;

			try {

				//Creates the file
				File output = new File(this.bullpenDir+" - "+loc+".html");
//				System.out.println("Should have created the file.");
				output.createNewFile();
				html = new BufferedWriter(new FileWriter(output));
				html.write("<html><h1> Location: "+loc + "</h1>");
				html.write("\n<h2>By: "+ per+ "</h2>");
				html.write("\n<table border= '1'>");
				html.write("\n<tr>");
				html.write("\n<th> Serial </th>");
				html.write("\n<th> Device </th>");
				html.write("\n<th> Model </th>");
				html.write("\n<th> Notes </th>");
				html.write("\n<th> Index </th>");
				html.write("\n</tr>");
				
				for(int i = 0; i<inventoryList.size(); ++i){
					html.write("\n<tr>");
					html.write("\n<td>"+inventoryList.get(i).getItemSerial() + "</td>");
					html.write("\n<td>"+inventoryList.get(i).getItemCategory()+"</td>");
					html.write("\n<td>"+inventoryList.get(i).getItemModel()+"</td>");
					html.write("\n<td>"+inventoryList.get(i).getItemNotes()+"</td>");
					html.write("\n<td>"+inventoryList.get(i).getIndexInt()+"</td>");
					html.write("\n</tr>");
					html.newLine();
												
				}
				html.write("</html>");
				html.close();
//				System.out.println("Finalized File Creation..");
				new JOptionPane("Built File");
			} //ends try
			catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();

				new JOptionPane("JAVA IO ERROR WHEN TRYING TO CREATE FILE");
			} // ends Try-Catch
		}// ends conditional
	}// ends constructor
}//ends class
